import turtle

pos_list = []
step = 200
turtle.speed(10)
def InitShape():
    global pos_list
    turtle.penup()
    turtle.backward(100)
    pos_list.append(turtle.pos())
    turtle.forward(step)
    pos_list.append(turtle.pos())
    turtle.left(120)
    turtle.forward(step)
    pos_list.append(turtle.pos())


def DrawKoch(n):
    global pos_list, step
    while n > 0:
        step /= 3
        new_pos_list = []
        for i in range(len(pos_list)):
            if i == len(pos_list) - 1:
                f = i
                t = 0
            else:
                f = i
                t = i + 1
            # f: from t: to
            turtle.setpos(pos_list[f])
            turtle.setheading(turtle.towards(pos_list[t]))
            new_pos_list.append(turtle.pos())
            turtle.forward(step)
            new_pos_list.append(turtle.pos())
            turtle.right(60)
            turtle.forward(step)
            new_pos_list.append(turtle.pos())
            turtle.left(120)
            turtle.forward(step)
            new_pos_list.append(turtle.pos())
            turtle.right(60)
            turtle.forward(step)
        pos_list = new_pos_list
        n -= 1
    
    new_pos_list.append(new_pos_list[0])
    turtle.setpos(new_pos_list[0])
    turtle.pendown()
    for i in range(1, len(new_pos_list)):
        turtle.goto(new_pos_list[i][0], new_pos_list[i][1])
        

InitShape()
DrawKoch(3)

turtle.mainloop()